
package com.android.test.activity;

import com.android.test.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnFocusChangeListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class HelpActivity extends Activity implements OnGestureListener {

    private ViewFlipper flipper;

    private GestureDetector detector;

    private static final int IMG_NUM = 3;

    private static int currentPage = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_help);

        detector = new GestureDetector(this);
        flipper = (ViewFlipper) this.findViewById(R.id.viewflipper_help);

        flipper.addView(addImageView(R.drawable.help_1));
        flipper.addView(addImageView(R.drawable.help_2));
        flipper.addView(addImageView(R.drawable.help_3));

        flipper.setOnFocusChangeListener(new OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {

            }
        });
    }

    private View addImageView(int id) {
        ImageView imgView = new ImageView(this);
        imgView.setBackgroundResource(id);
        return imgView;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.detector.onTouchEvent(event);
    }

    public boolean onDown(MotionEvent e) {
        return false;
    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (e1.getX() - e2.getX() > 30) {
            if (currentPage < IMG_NUM) {
                this.flipper
                        .setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
                this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_left_out));
                this.flipper.showNext();
            } else {
                currentPage = 1;
                jumptoActivityMain();
            }
            currentPage++;
            return true;
        } else if (e1.getX() - e2.getX() < -30) {
            if (currentPage > 1) {
                this.flipper.setInAnimation(AnimationUtils
                        .loadAnimation(this, R.anim.push_right_in));
                this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_right_out));
                this.flipper.showPrevious();
            }
            currentPage--;
            if (currentPage < 1) {
                currentPage = 1;
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            jumptoActivityMain();
        }
        return super.onKeyDown(keyCode, event);
    }

    public void onLongPress(MotionEvent e) {

    }

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    public void onShowPress(MotionEvent e) {

    }

    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    private void jumptoActivityMain() {

        detector = null;
        flipper = null;

        Intent intent = new Intent();
        intent.setClass(HelpActivity.this, MainEntryActivity.class);
        startActivity(intent);

        HelpActivity.this.finish();
    }
}
